package com.heima.user.service.impl;

import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.api.wemedia.IWmChannelClient;
import com.heima.common.constants.UserConstans;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserRealnameService;
import com.heima.user.mapper.ApUserRealnameMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author lg424
 * @description 针对表【ap_user_realname(APP实名认证信息表)】的数据库操作Service实现
 * @createDate 2024-11-26 19:53:56
 */
@Service
@RequiredArgsConstructor
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname>
        implements ApUserRealnameService {

    private final IWmChannelClient wmUserClient;
    private final ApUserMapper apUserMapper;

    @Override
    public ResponseResult listAll(AuthDto authDto) {
        authDto.checkParam();
        Page<ApUserRealname> page = new Page<>(authDto.getPage(), authDto.getSize());
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(authDto.getStatus() != null, ApUserRealname::getStatus, authDto.getStatus());
        wrapper.orderByDesc(ApUserRealname::getCreatedTime);
        getBaseMapper().selectPage(page, wrapper);
        List<ApUserRealname> records = page.getRecords();
        // 脱敏处理
        for (ApUserRealname record : records) {
            record.setIdno(DesensitizedUtil.idCardNum(record.getIdno(),3,4));
        }
        return ResponseResult.okResult(records);
    }

    @Override
    public ResponseResult authFail(AuthDto authDto) {
        if (StringUtils.isEmpty(authDto.getMsg())) {

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "需要给出失败原因");
        }
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(authDto.getId());
        apUserRealname.setStatus(UserConstans.TYPE_FAILD);
        apUserRealname.setReason(authDto.getMsg());
        apUserRealname.setUpdatedTime(new Date());
        getBaseMapper().updateById(apUserRealname);
        return ResponseResult.okResult(null);

    }

    @Override
    public ResponseResult authPass(AuthDto authDto) {
        // 检查参数是否有效
        if (authDto == null || authDto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "ID不能为空");
        }

        // 更新用户实名认证状态
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(authDto.getId());
        apUserRealname.setStatus(UserConstans.TYPE_SUCCESS);
        apUserRealname.setUpdatedTime(new Date());

        // 执行更新操作，检查更新结果
        if (getBaseMapper().updateById(apUserRealname) == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户实名认证记录不存在");
        }

        // 查询用户信息
        ApUser apUser = apUserMapper.selectById(apUserRealname.getUserId());
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
        }

        // 调用外部用户服务
        wmUserClient.saveUser(apUser);
        return ResponseResult.okResult(null);
    }


}




